package Frame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import Bullet.Bullet;
import Fish.Character;
import Fish.EnemyEll;
import Fish.EnemyHSF;
import Fish.EnemyMonk;
import Fish.EnemyShark;
import Fish.EnemyNimo;

public class GamePanel extends JPanel implements KeyListener{
	double oldTimeHSF = 0;
	double oldTimeShark=0;
	double oldTimeMonk=0;
	double oldTimeEll=0;
	double oldTimeNimo=0;
	int score=0;
	Character character = new Character(3,1);
	EnemyHSF[] hsf = new EnemyHSF[10];
	EnemyShark[] shark = new EnemyShark[3];
	EnemyEll[] ell = new EnemyEll[20];
	EnemyNimo[] nimo = new EnemyNimo[20];
	EnemyMonk monk = new EnemyMonk(7,10);
	
	JLabel lab_score = new JLabel("0");
	Bullet[] bullet = new Bullet[50];
	ImageIcon img_heart;
	JLabel lab_heart1,lab_heart2,lab_heart3;
	GamePanel(){
		lab_score.setForeground(new Color(200,200,255));
		lab_score.setFont(new Font("Dotum",Font.BOLD,40));
		lab_score.setSize(1000,100);
		lab_score.setLocation(1100, 0);
		
		img_heart = new ImageIcon("images/heart.png");
		lab_heart1 = new JLabel(img_heart);
		lab_heart2 = new JLabel(img_heart);
		lab_heart3 = new JLabel(img_heart);
		lab_heart1.setSize(62,50);
		lab_heart1.setLocation(50,10);
		lab_heart2.setSize(62,50);
		lab_heart2.setLocation(122,10);
		lab_heart3.setSize(62,50);
		lab_heart3.setLocation(194,10);
		
		add(lab_score);
		add(lab_heart1);
		add(lab_heart2);
		add(lab_heart3);
		add(character.getLabel());
		for(int i=0;i<10;i++){
			hsf[i] = new EnemyHSF(1,10);
			hsf[i].setY(Math.random()*600);
			hsf[i].setX(1300);
			add(hsf[i].getLabel());
			//Initialization Enemy fish - HSF.
		}
		for(int i=0;i<3;i++){
			shark[i] = new EnemyShark(2,50);
			shark[i].setY(Math.random()*600);
			shark[i].setX(1400);
			add(shark[i].getLabel());
			//Initialization Enemy fish - Shark.
		}
		for(int i=0;i<20;i++){
			ell[i] = new EnemyEll(1,10);
			ell[i].setY(Math.random()*600);
			ell[i].setX(1500);
			add(ell[i].getLabel());
			//Initialization Enemy fish - Shark.
		}
		for(int i=0;i<20;i++){
			nimo[i] = new EnemyNimo(1,10);
			nimo[i].setY(Math.random()*600);
			nimo[i].setX(1500);
			add(nimo[i].getLabel());
			//Initialization Enemy fish - Shark.
		}
		monk.setY(Math.random()*600);
		monk.setX(1700);
		add(monk.getLabel());
		for(int i=0;i<50;i++){
			bullet[i] = new Bullet();
			bullet[i].setLocation(-100,-100);
			add(bullet[i].getLabel());
		}
		setLayout(null);
		setPreferredSize(new Dimension(1300, 650));
        setVisible(false);
        
		ImageIcon img_background = new ImageIcon("images/background.png");
		JLabel lab_bg = new JLabel("", img_background,SwingConstants.CENTER);
		
		/** Background Image**/
		lab_bg.setSize(1300,650);
		lab_bg.setLocation(0,0);

		/** Add Objects in Panel**/
		setSize(1300,700);
		add(lab_bg);

		addKeyListener(this);
	}
	void setVisible(){
		setVisible(true);
		requestFocus();
	}
	@Override
	public void keyPressed(KeyEvent arg0) {
		int keycode = arg0.getKeyCode();
		int USER_MOVE = 10; //How much One step of Character
		switch (keycode) {
		case KeyEvent.VK_UP:
			if(character.getY()>-50)
				character.setLocation(character.getX(), character.getY()-USER_MOVE);break;
		case KeyEvent.VK_DOWN:
			if(character.getY()<500)
				character.setLocation(character.getX(), character.getY()+USER_MOVE);break;
		case KeyEvent.VK_LEFT:
			if(character.getX()>USER_MOVE)
				character.setLocation(character.getX()-USER_MOVE, character.getY());break;
		case KeyEvent.VK_RIGHT:
			if(character.getX()<1100-USER_MOVE)
				character.setLocation(character.getX()+USER_MOVE, character.getY());break;
		case KeyEvent.VK_SPACE:
			for(int i=0;i<50;i++){
				if(bullet[i].visible == false){
					bullet[i].visible=true;
					bullet[i].setDir(1);
					bullet[i].setLocation(character.getX()+200,character.getY());
					break;
				}
			}
			break;
		default: 
			break;
		}
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
	}
	
	public void updateHSF(double time){
		//HSF
		//oldtime[0] is HSF's.
		if(time-oldTimeHSF >= hsf[0].makeTime){
			oldTimeHSF=time;
			for(int i=0;i<hsf[0].maxNum;i++){
				if(hsf[i].visible == false){
					hsf[i].visible = true;
					break;
				}
			}
		}
		for(int i=0;i<hsf[0].maxNum;i++){//move HSF
			if(hsf[i].visible == true){
				hsf[i].setLocation(hsf[i].getX()-(int)hsf[i].speed, hsf[i].getY());
				if(hsf[i].getX() < -120){
					hsf[i].visible=false;
					hsf[i].setCount(hsf[i].hp);
					hsf[i].setY(Math.random()*600);
					hsf[i].setX(1300);
				}
			}
		}
		//HSF end------------------
	}
	public void updateShark(double time){
		//Shark
		//oldtime[0] is Shark's.
		if(time-oldTimeShark >= shark[0].makeTime){
			oldTimeShark=time;
			for(int i=0;i<shark[0].maxNum;i++){
				if(shark[i].visible == false){
					shark[i].visible = true;
					break;
				}
			}
		}
		for(int i=0;i<shark[0].maxNum;i++){//move Shark
			if(shark[i].visible == true){
				shark[i].setLocation(shark[i].getX()-(int)shark[i].speed, shark[i].getY());
				if(shark[i].getX() < -500){
					shark[i].visible=false;
					shark[i].setCount(shark[i].hp);
					shark[i].setY(Math.random()*600);
					shark[i].setX(1300);
				}
			}
		}
		//Shark end------------------
	}
	public void updateMonk(double time){
		//Shark
		//oldtime[0] is Shark's.
		if(time-oldTimeMonk >= monk.makeTime){
			oldTimeMonk=time;
			monk.visible=true;
		}
		
		if(monk.visible == true){
			monk.setLocation(monk.getX()-(int)monk.speed, monk.getY());
			if(monk.getX() < -800){
				monk.visible=false;
				monk.setCount(monk.hp);
				monk.setY(Math.random()*600);
				monk.setX(1300);
			}
		}
		//Shark end------------------
	}
	public void updateEll(double time){
		//Shark
		//oldtime[0] is Shark's.
		if(time-oldTimeEll >= ell[0].makeTime){
			oldTimeEll=time;
			for(int i=0;i<ell[0].maxNum;i++){
				if(ell[i].visible == false){
					ell[i].visible = true;
					break;
				}
			}
		}
		for(int i=0;i<ell[0].maxNum;i++){//move ell
			if(ell[i].visible == true){
				ell[i].setLocation(ell[i].getX()-(int)ell[i].speed, ell[i].getY());
				if(ell[i].getX() < -500){
					ell[i].visible=false;
					ell[i].setCount(ell[i].hp);
					ell[i].setY(Math.random()*600);
					ell[i].setX(1300);
				}
			}
		}
		//Shark end------------------
	}
	public void updateNimo(double time){
		//Nimo
		//oldtime[0] is Nimo's.
		if(time-oldTimeNimo >= nimo[0].makeTime){
			oldTimeNimo=time;
			for(int i=0;i<nimo[0].maxNum;i++){
				if(nimo[i].visible == false){
					nimo[i].visible = true;
					break;
				}
			}
		}
		for(int i=0;i<nimo[0].maxNum;i++){//move nimo
			if(nimo[i].visible == true){
				nimo[i].setLocation(nimo[i].getX()-(int)nimo[i].speed, nimo[i].getY());
				if(nimo[i].getX() < -500){
					nimo[i].visible=false;
					nimo[i].setCount(nimo[i].hp);
					nimo[i].setY(Math.random()*600);
					nimo[i].setX(1300);
				}
			}
		}
		//Nimo end------------------
	}
	public void updateBullet(){
		for(int i=0;i<50;i++){
			if(bullet[i].visible == true){
				bullet[i].setLocation(bullet[i].getX()+(bullet[i].getSpeed()*bullet[i].getDir()),bullet[i].getY());
				if(bullet[i].getX() < 0 || bullet[i].getX() > 1300)
					bullet[i].visible=false;
			}
		}		
	}
	public boolean crash(){
		int i,j;
		//HSF
		for(i=0;i<hsf[0].maxNum;i++){//move HSF
			if(hsf[i].visible == true){
				for(j=0;j<50;j++){
					if(hsf[i].getX()-10 < bullet[j].getX() && hsf[i].getX()+hsf[i].getWidth()+10 > bullet[j].getX() &&
						hsf[i].getY()-hsf[i].getHeight()-10 < bullet[j].getY() && hsf[i].getY()+10 > bullet[j].getY()){//�Ѿ��� ��� ����
						hsf[i].setCount(hsf[i].getCount()-1);
						if(hsf[i].getCount() <= 0){
							hsf[i].visible=false;
							hsf[i].setCount(hsf[i].hp);
							hsf[i].setY(Math.random()*600);
							hsf[i].setX(1300);
						}
						
						bullet[j].visible=false;
						bullet[j].setLocation(2000, 0);
					}
				}
			}
		}
		for(i=0;i<shark[0].maxNum;i++){//move shark
			if(shark[i].visible == true){
				for(j=0;j<50;j++){
					if(shark[i].getX()-10 < bullet[j].getX() && shark[i].getX()+shark[i].getWidth()+10 > bullet[j].getX() &&
						shark[i].getY()-shark[i].getHeight()-10 < bullet[j].getY() && shark[i].getY()+10 > bullet[j].getY()){//�Ѿ��� ��� ����
						score+=shark[i].getScore();
						shark[i].setCount(shark[i].getCount()-1);
						if(shark[i].getCount() <= 0){
							shark[i].visible=false;
							shark[i].setCount(shark[i].hp);
							shark[i].setY(Math.random()*600);
							shark[i].setX(1300);
						}						
						bullet[j].visible=false;
						bullet[j].setLocation(2000, 0);
					}
				}
			}
		}
		if(monk.visible == true){//monk
			for(j=0;j<50;j++){
				if(monk.getX()-10 < bullet[j].getX() && monk.getX()+monk.getWidth()+10 > bullet[j].getX() &&
					monk.getY()-monk.getHeight()-10 < bullet[j].getY() && monk.getY()+10 > bullet[j].getY()){//�Ѿ��� ��� ����
					score+=monk.getScore();
					monk.setCount(monk.getCount()-1);
					if(monk.getCount() <= 0){
						monk.visible=false;
						monk.setCount(monk.hp);
						monk.setY(Math.random()*600);
						monk.setX(1300);
					}
					
					bullet[j].visible=false;
					bullet[j].setLocation(2000, 0);
				}
			}
		}
		for(i=0;i<ell[0].maxNum;i++){//move ell
			if(ell[i].visible == true){
				for(j=0;j<50;j++){
					if(ell[i].getX()-10 < bullet[j].getX() && ell[i].getX()+ell[i].getWidth()+10 > bullet[j].getX() &&
						ell[i].getY()-ell[i].getHeight()-10 < bullet[j].getY() && ell[i].getY()+10 > bullet[j].getY()){//�Ѿ��� ��� ����
						score+=ell[i].getScore();
						ell[i].setCount(ell[i].getCount()-1);
						if(ell[i].getCount() <= 0){
							ell[i].visible=false;
							ell[i].setCount(ell[i].hp);
							ell[i].setY(Math.random()*600);
							ell[i].setX(1300);
						}
						
						bullet[j].visible=false;
						bullet[j].setLocation(2000, 0);
					}
				}
			}
		}
		for(i=0;i<nimo[0].maxNum;i++){//move nimo
			if(nimo[i].visible == true){
				for(j=0;j<50;j++){
					if(nimo[i].getX()-10 < bullet[j].getX() && nimo[i].getX()+nimo[i].getWidth()+10 > bullet[j].getX() &&
						nimo[i].getY()-nimo[i].getHeight()-10 < bullet[j].getY() && nimo[i].getY()+10 > bullet[j].getY()){//�Ѿ��� ��� ����
						score+=nimo[i].getScore();
						nimo[i].setCount(nimo[i].getCount()-1);
						if(nimo[i].getCount() <= 0){
							nimo[i].visible=false;
							nimo[i].setCount(nimo[i].hp);
							nimo[i].setY(Math.random()*600);
							nimo[i].setX(1300);
						}
						
						bullet[j].visible=false;
						bullet[j].setLocation(2000, 0);
					}
				}
			}
		}
		
		for(i=0;i<hsf[0].maxNum;i++){
			character.crash(hsf[i]);
		}
		for(i=0;i<shark[0].maxNum;i++){
			character.crash(shark[i]);
		}
		character.crash(monk);
		for(i=0;i<ell[0].maxNum;i++){
			character.crash(ell[i]);
		}
		for(i=0;i<nimo[0].maxNum;i++){
			character.crash(nimo[i]);
		}
		if(character.getCount() ==2){
			lab_heart3.setVisible(false);
		}else if(character.getCount()==1){
			lab_heart2.setVisible(false);
		}else if(character.getCount() <= 0){
			lab_heart1.setVisible(false);
			return true;
		}
		return false;
	}
	public void updateScore(){
		lab_score.setText(score+"");
	}
}
